﻿package com.badibadi.fragment;

import java.util.Map;

import net.tsz.afinal.FinalBitmap;

import com.badibadi.uniclubber.R;
import com.badibadi.infos.ExpertApplyChoseTypePictureInfos;
import com.badibadi.mytools.BitmapHelp;
import com.badibadi.mytools.Utils;
//import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 注意：选中的数据将保存在ChoseData
 * 
 * @author Administrator
 * 
 */
public class ExpertAppleyForChoseTypeFragment extends BaseFragment {
	/**
	 * 存放TextView图片ChoseData中
	 */
	private ImageView img[];
	// RadioGroup radioGroup;
	/**
	 * 每页的数量
	 */
	private final int IMGNUMBER = 9;
	/**
	 * 遮罩
	 */
	private ImageView img_zhezhao[];
	/**
	 * 文字
	 */
	private TextView tv[];
	/**
	 * 图片钩的id
	 */
	private ImageView img_gou[];

	private TableRow tableRow[];
	// 下面的是数据
	/**
	 * 要显示的图片名
	 */
	private String item_text[];
	/**
	 * 要显示的图片
	 */
	private String picture[];
	/**
	 * 保存点击后选中的数据
	 */
	private String ChoseData = "";
	/**
	 * 记录选中项
	 */
	private int Choseterm = -1;

	/**
	 * 记录点击的是当前九宫格中的第几个
	 */
	private int jiugonggeitem = -1;
	private int yeshu;
	/**
	 * 总页数(无0)
	 */
	private int zongyeshu;
	/**
	 * 记录当前页数(有0)
	 */
	private int ThisPage = 0;
	/**
	 * (无效了) 可复用界面标记：根据标记来初始化数据 0:达人申请选择达人类型界面 1：创建新的俱乐部 2：发布活动
	 */
	private int Flags = 0;

	private FinalBitmap finalBitmap;
	private DisplayImageOptions options;
	/**
	 * 图片的宽高
	 */
	private int PictureWH;
	/**
	 * View view
	 */
	View view;
	/** 用来设置资料 */
	private OnDataChangeClistener onDataClistener;

	private String imgPath = "assets/imgs/type_bg.jpg";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		finalBitmap = FinalBitmap.create(getActivity());
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(0)).build();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		view = inflater.inflate(R.layout.fragment_expert_appley_chose_type,
				null);
		initView(view);
		initData();
		/**
		 * 可以用作回调来刷新界面
		 */
		initExpertAppleyForChoseTypeView();
		// getNeedAddRadioButtonNumber();
		initializationInterface(view);
		// RefreshRadioGroup();
		// tableRow[0].setLayoutParams(l);
		return view;
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		// TODO Auto-generated method stub
		if (Flags == 0) {
			ExpertApplyChoseTypePictureInfos experInfos = new ExpertApplyChoseTypePictureInfos(
					getActivity());
			// item_text = experInfos.getItem();
			picture = experInfos.getPic();

		}
		if (onDataClistener != null) {
			item_text = onDataClistener.getTuPianMing(item_text);
//			   
			zongyeshu = getAllPage();
			// for(int i=0;i<item_text.length;i++)
			// {
			//    
			// }
		}
	}

	/**
	 * 根据数据初始化界面的侦听器(文本数量和图片数量必须相匹配)
	 */
	private void initializationInterface(final View view) {
		// TODO Auto-generated method stub

		for (int i = 0; i < img.length; i++) {
			final int pos = i;
			img[i].setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub

					// ChoseData=item_text[(ThisPage+1)*position];
					//    
					setSelect(pos);

				}
			});
		}

		// radioGroup.setOnCheckedChangeListener(new
		// RadioGroup.OnCheckedChangeListener() {
		//
		// @Override
		// public void onCheckedChanged(RadioGroup group, int checkedId) {
		// // TODO Auto-generated method stub
		// //    
		// if(group.getChildAt(0).getId()==R.id.feac_radio0)
		// {
		// ThisPage=0;
		// initExpertAppleyForChoseTypeView();
		// }
		// //子数量
		// int number=group.getChildCount();
		// for(int i=1;i<number;i++)
		// {
		// if(group.getCheckedRadioButtonId()==i)
		// {
		// ThisPage=i;
		// initExpertAppleyForChoseTypeView();
		// }
		// }
		// switch(group.getCheckedRadioButtonId())
		// {
		// case 1:
		// ThisPage=1;
		// initExpertAppleyForChoseTypeView(view);
		// // Toast.makeText(getActivity(), "再次成功", 1).show();
		// break;
		// case 2:
		// ThisPage=2;
		// initExpertAppleyForChoseTypeView(view);
		// break;
		// }
		//
		// }
		// });

	}

	/**
	 * 设置选中项目（改变选中项的视图）和重置其他视图
	 */
	public void setSelect(int position) {
		// 遍历每一张视图，将position的位置打钩
		for (int i = 0; i < IMGNUMBER; i++) {
			// 如果i不等于选中项
			if (i != position) {
				img_zhezhao[i].setVisibility(View.VISIBLE);
				img_gou[i].setVisibility(View.INVISIBLE);
			} else if (i == position) {
				img_zhezhao[i].setVisibility(View.INVISIBLE);
				img_gou[i].setVisibility(View.VISIBLE);
				if (ThisPage * IMGNUMBER + i < picture.length) {
					// 将当前页的数据存入
					ChoseData = item_text[ThisPage * IMGNUMBER + i];
					Choseterm = ThisPage * IMGNUMBER + i;
				} else {
					ChoseData = "";
				}
				//    
				// "ChoseData===>"+ChoseData+"Choseterm======>"+Choseterm);
			}

		}
		jiugonggeitem = position;
//		   
	}

	// @Override
	// public void onResume() {
	// // TODO Auto-generated method stub
	// finalBitmap.setExitTasksEarly(false);
	// super.onResume();
	//
	//
	// }
	// @Override
	// public void onPause() {
	// // TODO Auto-generated method stub
	// finalBitmap.setExitTasksEarly(true);
	// super.onPause();
	//
	// }
	//
	// @Override
	// public void onDestroy() {
	// // TODO Auto-generated method stub
	// // Utils.ExitPrgress(MyDataActivity.this);
	// finalBitmap.closeCache();
	// super.onDestroy();
	//
	// }
	/**
	 * 可以用作回调来刷新界面(文本数量和图片数量必须相匹配)
	 * 
	 * @param view
	 */
	public void initExpertAppleyForChoseTypeView() {
		// TODO Auto-generated method stub
		//    
		if (ThisPage == zongyeshu - 1) {
			// 不满9张的数量
			int buman9 = getRedundantImageNumber();
			//    
			/**
			 * 初始化所有图片
			 */
			for (int i = 0; i < buman9; i++) {
				final int ii = i;
				tv[i].setText(item_text[IMGNUMBER * ThisPage + i]);
				if (finalBitmap != null) {
					// img[i].getViewTreeObserver().addOnGlobalLayoutListener(new
					// ViewTreeObserver.OnGlobalLayoutListener() {
					//
					// @Override
					// public void onGlobalLayout() {
					// // TODO Auto-generated method stub
					// PictureWH=img[ii].getWidth();
					// }
					// });

					// finalBitmap.display(img[i], picture[IMGNUMBER * ThisPage
					// +
					// i],PictureWH,PictureWH,BitmapFactory.decodeResource(getActivity().getResources(),
					// R.drawable.etest1),BitmapFactory.decodeResource(getActivity().getResources(),
					// R.drawable.etest1));
					// ImageLoader.getInstance().displayImage(picture[IMGNUMBER
					// * ThisPage + i], img[i], options);
					// BitmapHelp.getBitmapUtils(getActivity()).display(img[i],
					// imgPath, BitmapHelp.getBitmapOptions(getActivity()));

					BitmapHelp bitmap = new BitmapHelp();
					bitmap.displayImage(getActivity(), img[i], imgPath);

				}
				// img[i].setBackgroundDrawable(picture[IMGNUMBER * ThisPage +
				// i]);
				img_gou[i].setVisibility(View.INVISIBLE);
				img_zhezhao[i].setVisibility(View.VISIBLE);

				ChoseData = "";
				Choseterm = -1;
			}
			for (int j = IMGNUMBER - 1; j >= buman9; j--) {
				tv[j].setText("");
				img[j].setBackgroundResource(R.drawable.etest1);
				img_gou[j].setVisibility(View.INVISIBLE);
				img_zhezhao[j].setVisibility(View.VISIBLE);

				ChoseData = "";
				Choseterm = -1;
			}
			return;
		}
		/**
		 * 初始化所有图片
		 */
		for (int i = 0; i < IMGNUMBER; i++) {
			final int ii = i;
			//    
			// "IMGNUMBER*ThisPage+i===>"+((IMGNUMBER*ThisPage)+i));
			tv[i].setText(item_text[(IMGNUMBER * ThisPage) + i]);
			if (finalBitmap != null) {
				// img[i].getViewTreeObserver().addOnGlobalLayoutListener(new
				// ViewTreeObserver.OnGlobalLayoutListener() {
				//
				// @Override
				// public void onGlobalLayout() {
				// // TODO Auto-generated method stub
				// PictureWH=img[ii].getWidth();
				// }
				// });

				// finalBitmap.display(img[i], picture[IMGNUMBER * ThisPage +
				// i],PictureWH,PictureWH,BitmapFactory.decodeResource(getActivity().getResources(),
				// R.drawable.etest1),BitmapFactory.decodeResource(getActivity().getResources(),
				// R.drawable.etest1));
				// ImageLoader.getInstance().displayImage(picture[IMGNUMBER *
				// ThisPage + i], img[i], options);
				// BitmapHelp.getBitmapUtils(getActivity()).display(img[i],
				// imgPath, BitmapHelp.getBitmapOptions(getActivity()));
				BitmapHelp bitmap = new BitmapHelp();
				bitmap.displayImage(getActivity(), img[i], imgPath);
			}
			img_gou[i].setVisibility(View.INVISIBLE);
			img_zhezhao[i].setVisibility(View.VISIBLE);

			ChoseData = "";
			Choseterm = -1;
		}
	}

	// /**
	// * 得到需要添加的radio数量并且添加radiobutton
	// * @return
	// */
	// public int getNeedAddRadioButtonNumber()
	// {
	// int number=item_text.length;
	// if(number/IMGNUMBER>=1)
	// {
	// //    
	// yeshu=number/IMGNUMBER;
	// if(number%IMGNUMBER>0)
	// {
	// yeshu=yeshu+1;
	// }
	// }
	// int addyeshu=yeshu-1;
	// for(int i=1;i<=addyeshu;i++)
	// {
	// //    
	// RadioButton radioButton=new RadioButton(getActivity());
	// radioButton.setTag("feac_radio"+i);
	// radioButton.setId(i);
	// radioButton.setText("");
	// radioButton.setClickable(false);
	// radioGroup.addView(radioButton);
	// }
	// radioGroup.getChildAt(0).setClickable(false);
	// return addyeshu;
	//
	// }

	//
	//
	// @Override
	// public void onDestroy() {
	// // TODO Auto-generated method stub
	// if(finalBitmap!=null)
	// {
	// finalBitmap.clearCache();
	// // finalBitmap.closeCache();
	// }
	// super.onDestroy();
	// }
	/**
	 * 不满九张多余的图片的数量
	 * 
	 * @return
	 */
	private int getRedundantImageNumber() {
		int number = item_text.length;
		// if(number%IMGNUMBER>0)
		//
		// }
		number = number % IMGNUMBER;
		return number;
	}

	// public void RefreshRadioGroup()
	// {
	// //   
	// radioGroup.check(radioGroup.getChildAt(ThisPage).getId());
	// //radioGroup.getChildAt(ThisPage).setClickable(false);
	//
	// }
	/**
	 * 初始化id
	 * 
	 * @param view
	 */
	private void initView(View view) {
		// TODO Auto-generated method stub
		img = new ImageView[] { (ImageView) view.findViewById(R.id.feac_img0),
				(ImageView) view.findViewById(R.id.feac_img1),
				(ImageView) view.findViewById(R.id.feac_img2),
				(ImageView) view.findViewById(R.id.feac_img3),
				(ImageView) view.findViewById(R.id.feac_img4),
				(ImageView) view.findViewById(R.id.feac_img5),
				(ImageView) view.findViewById(R.id.feac_img6),
				(ImageView) view.findViewById(R.id.feac_img7),
				(ImageView) view.findViewById(R.id.feac_img8),

		};
		img_zhezhao = new ImageView[] {
				(ImageView) view.findViewById(R.id.feac_zhezhao0),
				(ImageView) view.findViewById(R.id.feac_zhezhao1),
				(ImageView) view.findViewById(R.id.feac_zhezhao2),
				(ImageView) view.findViewById(R.id.feac_zhezhao3),
				(ImageView) view.findViewById(R.id.feac_zhezhao4),
				(ImageView) view.findViewById(R.id.feac_zhezhao5),
				(ImageView) view.findViewById(R.id.feac_zhezhao6),
				(ImageView) view.findViewById(R.id.feac_zhezhao7),
				(ImageView) view.findViewById(R.id.feac_zhezhao8), };
		tv = new TextView[] { (TextView) view.findViewById(R.id.feac_text0),
				(TextView) view.findViewById(R.id.feac_text1),
				(TextView) view.findViewById(R.id.feac_text2),
				(TextView) view.findViewById(R.id.feac_text3),
				(TextView) view.findViewById(R.id.feac_text4),
				(TextView) view.findViewById(R.id.feac_text5),
				(TextView) view.findViewById(R.id.feac_text6),
				(TextView) view.findViewById(R.id.feac_text7),
				(TextView) view.findViewById(R.id.feac_text8), };
		img_gou = new ImageView[] {
				(ImageView) view.findViewById(R.id.feac_gou0),
				(ImageView) view.findViewById(R.id.feac_gou1),
				(ImageView) view.findViewById(R.id.feac_gou2),
				(ImageView) view.findViewById(R.id.feac_gou3),
				(ImageView) view.findViewById(R.id.feac_gou4),
				(ImageView) view.findViewById(R.id.feac_gou5),
				(ImageView) view.findViewById(R.id.feac_gou6),
				(ImageView) view.findViewById(R.id.feac_gou7),
				(ImageView) view.findViewById(R.id.feac_gou8), };
		tableRow = new TableRow[] { (TableRow) view
				.findViewById(R.id.feac_tableRow1),

		};
		// radioGroup=(RadioGroup)view.findViewById(R.id.feac_radioGroup1);
	}

	/**
	 * 获取总页数
	 */
	private int getAllPage() {
		int pagenumber = ((item_text.length / 9) + 1);
		return pagenumber;
	}

	public int getZongyeshu() {
		return zongyeshu;
	}

	public void setZongyeshu(int zongyeshu) {
		this.zongyeshu = zongyeshu;
	}

	public int getThisPage() {
		return ThisPage;
	}

	public void setThisPage(int thisPage) {
		ThisPage = thisPage;
	}

	public String getChoseData() {
		return ChoseData;
	}

	public void setChoseData(String choseData) {
		ChoseData = choseData;
	}

	public void getJiugongItem(CallBack callBack) {
		// callBack.getMessage(ThisPage,jiugonggeitem);
		callBack.getFragmentView(view, ThisPage, jiugonggeitem, img_gou,
				img_zhezhao, ChoseData);
	}

	public void getChoseData(CallBack2 callBack) {
		callBack.getData(ChoseData);
	}

	public interface CallBack {
		// public void getMessage(int ThisPage,int position);
		public void getFragmentView(View view, int Page, int jiugonggeitem,
				ImageView[] img_gou, ImageView[] img_zhezhao, String ChoseData);
	}

	public void getFlags(CallBack2 callback2) {
		callback2.getData("" + Flags);
	}

	public interface CallBack2 {
		public void getData(String Data);
	}

	public void setOnDataClistener(OnDataChangeClistener listener) {
		onDataClistener = listener;
	}

	public interface OnDataChangeClistener {
		String[] getTuPianMing(String[] tupianming);

		String[] getBeiJingTu(String[] beijingtu);

	}
}
